package com.sptpc.stu.controller;

import cn.hutool.core.util.StrUtil;
import com.sptpc.stu.common.controller.BaseController;
import com.sptpc.stu.common.response.JSONResponse;
import com.sptpc.stu.common.utils.QueryPage;
import com.sptpc.stu.controller.request.*;
import com.sptpc.stu.model.my.User;
import com.sptpc.stu.repository.my.UserRepository;
import com.sptpc.stu.service.MailService;
import com.sptpc.stu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Optional;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserRepository userRepository;


    @CrossOrigin
    @GetMapping(value = "/getList")
    private JSONResponse toGetAllStudent()
    {
        return userService.getAllUser();
    }

    @CrossOrigin
    @GetMapping(value = "/searchLike")
    private JSONResponse toSearchNameLike(String name)
    {
        return JSONResponse.ofSuccess(userRepository.findUsersPageByNameLike(name+"%"+"%"));
    }

    @CrossOrigin
    @GetMapping(value = "/searchUid")
    private JSONResponse toSearchUid(Long uid)
    {
        return JSONResponse.ofSuccess(userRepository.findUserByUid(uid));
    }


    @GetMapping(value = "/getRandom")
    private JSONResponse toRandomGetStudent()
    {
        return userService.getRandomUserinfo();
    }



    @PutMapping(value = "/updateBasic")
    private JSONResponse toUpdateUserInfo(@RequestBody UserBasicInfoRequest changeBasicInfoRequest)
    {
        return userService.updateUserBasicInfo(changeBasicInfoRequest);
    }

    @PutMapping(value = "/updateLogin")
    private JSONResponse toUpdateUserInfo(@RequestBody UserLoginInfoRequest changeBasicInfoRequest)
    {
        return userService.updateUserLoginInfo(changeBasicInfoRequest);
    }

    @DeleteMapping(value = "/deleteUser/{uid}")
    private JSONResponse toDeleteUserbyUid(@PathVariable Long uid){
        return userService.deleteUser(uid);
    }


}
